package com.controller.admin;

import com.entity.Order;
import com.service.admin.AdminCommonService;
import com.service.admin.AdminLoginService;
import com.service.admin.AdminOrderService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 所有管理员请求的控制器，请求全部为post请求
 *
 * 
 */
@RestController
@CrossOrigin
public class AdminCommonController {

  @Autowired AdminLoginService adminLoginService;
  @Autowired AdminOrderService adminOrderService;
  @Autowired AdminCommonService adminCommonService;

  /**
   * 管理员登录
   *
   * @param name 登录用户名
   * @param pswd 登录密码
   * @return 登陆成功返回1，登陆失败返回0
   */
  @PostMapping("admin/login")
  public int login(@RequestParam("name") String name, @RequestParam("pswd") String pswd) {
    return adminLoginService.login(name, pswd);
  }

  /**
   * 获取所有用户数量
   *
   * @return 所有用户数量
   */
  @PostMapping("admin/getAllOrderCount")
  public String getAllOrderCount(@RequestParam("orderStatus") int orderStatus) {
    int count = adminOrderService.getAllOrderCount(orderStatus);
    return String.valueOf(count);
  }

  @PostMapping("admin/getAllFeedback")
  List<Map<String, Object>> getAllFeedback(
      @RequestParam("pageSize") int pageSize, @RequestParam("pageIndex") int pageIndex) {
    return adminCommonService.getAllFeedBack(pageSize, pageIndex);
  }

  @PostMapping("admin/setFeedbackSeen")
  public String setFeedbackSeen(@RequestParam("id") int id) {
    int ret = adminCommonService.setFeedBackSeen(id);
    return String.valueOf(ret);
  }
  /**
   * 获取所有订单
   *
   * @param orderStatus 订单状态，为0表示所有订单
   * @param pageSize 页大小
   * @param pageIndex 页索引
   * @return 订单列表
   */
  @PostMapping("admin/getAllOrders")
  public List<Order> getAllOrders(
      @RequestParam("orderStatus") int orderStatus,
      @RequestParam("pageSize") int pageSize,
      @RequestParam("pageIndex") int pageIndex) {
    return adminOrderService.getAllOrders(orderStatus, pageSize, pageIndex);
  }

  @GetMapping("getArrayIndex")
  public String getArrayIndex(@RequestParam("array[]") String[] array) {
    return "getArrayIndex---" + Arrays.toString(array);
  }

  @GetMapping("getArray")
  public String getArray(@RequestParam("array") String[] array) {
    return "getArray---" + Arrays.toString(array);
  }

  @PostMapping("postArray")
  public String postArray(@RequestParam("array") String[] array) {
    return "postArray" + Arrays.toString(array);
  }

  @PostMapping("postArrayIndex")
  public String postArrayIndex(@RequestParam("array[]") String[] array) {
    return "postArrayIndex---" + Arrays.toString(array);
  }
}
